<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // 对象的常用方法
      var obj = { name: "老董", age: 28, job: "老师" };
      //   键值对{key:value}
      Object.keys(obj);
      console.log(Object.keys(obj));
      console.log(Object.values(obj));
      console.log(Object.entries(obj));
      var arr = [
        ["name", "老李"],
        ["age", 26],
      ];
      console.log(Object.fromEntries(arr));

      // 数组的常用方法
      var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
      // 过滤 filter---返回值是数组
      console.log(arr.filter((i) => i % 2 === 0));
      // 查找 find findIndex some includes
      console.log(arr.find((i) => i === 4)); //返回满足条件的第一个一项
      console.log(arr.findIndex((i) => i === 10)); //-1找到就是返回第一个索引，找不到就返回-1
      console.log(arr.some((i) => !isNaN(i))); //是不是数字 返回布尔值
      console.log(arr.some((i) => /\d/.test(i))); //是不是数字 返回布尔值
      console.log(arr.includes(4)); //true 返回布尔值
      // 遍历 for in ,forEach, map
      //map 不会修改原始数据，返回一个数组
      arr.map((value) => {
        return value;
      });
      // 累积计算
      var arr2 = [
        { name: "苹果", $: 100, n: 5 },
        { name: "香蕉", $: 80, n: 3 },
      ];
      const result = arr2.reduce((a, b) => {
        return a + b.$ * b.n;
      }, 0);
      console.log(result);
      // 排序
      var arr3 = [100, 5, 40, 10, 11, 2, 59, 789, 11111];
      var arr4 = [{ a: 1 }, { a: 5 }, { a: 100 }];
      arr4.sort((a, b) => {
        return a.a - b.a;
      });
      console.log(arr4);
      // 扁平化
      var arr5 = [
        [1, 1, 2, [3, 4]],
        [5, 6, [7, 8, [9, 10]]],
      ];
      var arr6 = arr5.flat(Infinity);
      console.log(arr6);
      console.log(Array.from(new Set(arr6)));
    </script>
  </body>
</html>
